package com.md.youyouhui.common;

import android.support.annotation.Nullable;


/**
 * 用于{@link android.arch.lifecycle.LiveData}对外暴露请求进度,比如下载文件
 * <p>
 * 不同于 {@link ProgressResource} ,此类具有第二进度
 * <p>
 * <p>
 * 如果仅需要知道是否处于请求中,请使用{@link Resource}
 * <p>
 * 一旦创建,数据不允许更改
 * 每次使用都会创建新的实例
 *
 * @author yawei
 * @data on 17-11-10  上午10:58
 * @project furling3d
 * @org www.komlin.com
 * @email zyawei@live.com
 */

public final class FullProgressResource<T> {


    public static final int START = 0;
    public static final int PROGRESS = 1;
    public static final int FINISH = 2;
    public static final int ERROR = 3;


    /**
     * 第一进度
     */
    public final int progress;

    /**
     * 第一最大值
     */
    public final int max;

    /**
     * 第二进度
     */
    public final int secondProgress;

    /**
     * 第二进度最大值
     */
    public final int secondMax;

    /**
     * 中进度
     */
    public final int allMax;
    /**
     * 进度
     * {@link #START}表示开始
     * {@link #FINISH}表示成功
     * {@link #ERROR}表示失败
     */
    public final int state;


    /**
     * 只有{@link #progress} == {@link #ERROR} 时才会此code才有效
     * <p>
     * 由数据源提供code 这里只是传递
     * <p>
     * eg: http.errorCode
     * </p>
     */
    public final int errorCode;

    /**
     * 数据
     */
    @Nullable
    public final T data;

    private FullProgressResource(@Nullable T data, int state, int progress, int max, int secondProgress, int secondMax, int allMax, int errorCode) {
        this.progress = progress;
        this.secondProgress = secondProgress;
        this.secondMax = secondMax;
        this.allMax = allMax;
        this.state = state;
        this.errorCode = errorCode;
        this.data = data;
        this.max = max;
    }

    public static <S> FullProgressResource<S> start() {
        return new FullProgressResource<>(null, START, 0, 0, 0, 0, 0, 0);
    }

    public static <S> FullProgressResource<S> start(@Nullable S data, int max, int secondMax, int allMax) {
        return new FullProgressResource<>(data, START, 0, max, 0, secondMax, allMax, 0);
    }

    public static <S> FullProgressResource<S> progress(@Nullable S data, int progress, int max, int secondProgress, int secondMax, int allMax) {
        return new FullProgressResource<>(data, PROGRESS, progress, max, secondProgress, secondMax, allMax, 0);
    }

    public static <S> FullProgressResource<S> finish(@Nullable S data, int max, int secondMax, int allMax) {
        return new FullProgressResource<>(data, FINISH, 0, max, 0, secondMax, allMax, 0);
    }

    public static <S> FullProgressResource<S> error(@Nullable S data) {
        return new FullProgressResource<>(data, ERROR, 0, 0, 0, 0, 0, 0);
    }

}
